<!DOCTYPE html>
<html>
<head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>吕梁健康体检预约系统</title>
        <meta name="description" content="吕梁健康体检预约系统">
        <meta name="keywords" content="吕梁健康体检预约系统">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">

        <!-- 引入组件库 -->
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/vue.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <script src="../js/axios-0.18.0.js"></script>

    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
        .add-form{
            width: 50%;
        }
        .boxContainer{
            display: flex;
            width: 80%;
            flex-wrap: wrap;
            justify-content: space-between;
        }
        .avatarBox{
            position: relative;
            width: 178px;
            margin-bottom: 15px;
        }
        .avatarBox img{
            width: 100%;
        }
        .avatarBox .del{
            position:absolute;
            top: 0;
            right: 0;
            color: #ccc;
        }
    </style>
        </head>
        <body class="hold-transition">
        <div id="app">
            <div class="content-header">
        <h1>报告上传
            <small>报告上传</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>报告上传</el-breadcrumb-item>
            <el-breadcrumb-item>报告上传</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="档案号/姓名/手机号" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
<!--                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>-->
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="physicalexamtime" label="体检时间" align="center" type="date" pattern="yyyy-MM-dd"
                                 :formatter="formatter"></el-table-column>
                <el-table-column prop="memberId" label="会员id" align="center"></el-table-column>
<!--                <el-table-column prop="filenumber" label="档案号" align="center"></el-table-column>-->
                <el-table-column prop="name" label="姓名" align="center"></el-table-column>
                <el-table-column prop="phonenumber" label="手机号" align="center"></el-table-column>
                <el-table-column prop="setmeal" label="体检套餐" align="center"></el-table-column>
                <el-table-column prop="orderStatus" label="报告状态" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">上传报告</el-button>
<!--                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>-->
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 上传报告标签弹层 -->
            <div class="add-form">
                <el-dialog title="上传报告" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12" label="体检日期">
                                <el-form-item label="体检日期" prop="physicalexamtime">
                                    <el-input v-model="formData.physicalexamtime" disabled="disabled"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="档案号" prop="filenumber">
                                    <el-input v-model="formData.filenumber" disabled="disabled"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="姓名" prop="name">
                                    <el-input v-model="formData.name" disabled="disabled"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="手机号" prop="phonenumber">
                                    <el-input v-model="formData.phonenumber" disabled="disabled"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="20">
                                <el-form-item label="体检套餐" prop="setmeal">
                                    <el-input v-model="formData.setmeal" disabled="disabled"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传图片" prop="report">
                                    <!--
                                      el-upload：上传组件
                                      action：上传的提交地址
                                      auto-upload：选中文件后是否自动上传
                                      name：上传文件的名称，服务端可以根据名称获得上传的文件对象
                                      show-file-list：是否显示已上传文件列表
                                      on-success：文件上传成功时的钩子
                                      before-upload：上传文件之前的钩子
                                    -->
                                    <el-upload
                                            class="avatar-uploader"
                                            :action="backend_url+'/tPhyexamrecord/upload'"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <!--用于上传图片预览-->
<!--                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">-->
                                        <!--<div v-for="(item,idx) in imgArrays" :key="idx">
                                            <img v-if="item" :src="item" class="avatar">
                                        </div>-->

                                        <div class="boxContainer">
                                            <div v-for="(item,idx) in imgArrays" :key="idx" class="avatarBox">
                                                <img v-if="item" :src="item" class="avatar">
                                                <i class="el-icon-close del" @click.stop="closePreview(idx)"></i>
                                            </div>
                                        </div>

                                        <!--用于展示上传图标-->
                                        <i  class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">保存</el-button>
                        <el-button type="primary" @click="handleSubmit()">提交</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    // axios跨域访问，如果需要携带cookie，withCredentials必须为true
    axios.defaults.withCredentials = true;
    var vue = new Vue({
        el: '#app',
        data: {
            imgArrays:[],
            backend_url: "http://localhost:9002",
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                phoneNumber: [{required: true, message: '手机号为必填项', trigger: 'blur'}],
                name: [{required: true, message: '会员名为必填项', trigger: 'blur'}],
                fileNumber: [{required: true, message: '档案号为必填项', trigger: 'blur'}],
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage(1);
        },
        methods: {
            closePreview(idx){
                this.imgArrays.splice(idx,1);
            },
            //图片上传完成后自动执行
            handleAvatarSuccess(response, file, fileList) {
                // 与axios的请求回调参数不同，这里response <==> Result
                this.imageUrl = response.data;
                this.$message({
                    message: response.message,
                    type: response.flag ? 'success' : 'error'
                });
                //设置模型数据（图片名称），后续提交ajax请求时会提交到后台最终保存到数据库
                // this.formData.report = response.data;
                // this.$set(this.formData,'report',response.data);
                this.imgArrays.push(response.data);
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            formatter(row, col, cellValue, index) {
                var date = new Date(cellValue);
                var year = date.getFullYear();
                var month = (date.getMonth() + 1) < 10 ? ('0' + (date.getMonth() + 1)) : (date.getMonth() + 1);
                var day = date.getDay() < 10 ? '0' + date.getDay() : date.getDay();
                cellValue = year + '-' + month + '-' + day;
                return cellValue
            },
            //编辑
            handleEdit() {
                this.$refs["dataEditForm"].validate((valid) => {
                    if (valid) {
                        //校验通过
                        this.formData.report=JSON.stringify(this.imgArrays);
                        axios.post(this.backend_url + "/tPhyexamrecord/edit", this.formData).then((response) => {
                            if (response.data.flag) {
                                //请求成功
                                this.$message.success(response.data.message);
                            } else {
                                //请求失败
                                this.$message.error(response.data.message);
                            }
                        }).catch((error) => {
                            console.log(error);
                            this.$message.error("网络异常");
                        }).finally(() => {
                            //刷新数据
                            this.findPage(1);
                            //隐藏新增窗口
                            this.dialogFormVisible4Edit = false;
                        });

                    } else {
                        //校验未通过
                        this.$message.error("编辑检查项-数据校验未通过")
                    }
                })

            },
            //提交
            handleSubmit() {
                this.$refs["dataEditForm"].validate((valid) => {
                    if (valid) {
                        //校验通过
                        this.formData.orderStatus="报告已上传";
                        this.formData.report=JSON.stringify(this.imgArrays);
                        axios.post(this.backend_url + "/tPhyexamrecord/edit", this.formData).then((response) => {
                            if (response.data.flag) {
                                //请求成功
                                this.$message.success(response.data.message);
                            } else {
                                //请求失败
                                this.$message.error(response.data.message);
                            }
                        }).catch((error) => {
                            console.log(error);
                            this.$message.error("网络异常");
                        }).finally(() => {
                            //刷新数据
                            this.findPage(1);
                            //隐藏新增窗口
                            this.dialogFormVisible4Edit = false;
                        });

                    } else {
                        //校验未通过
                        this.$message.error("编辑检查项-数据校验未通过")
                    }
                })

            },
            //添加
            handleUpdate(row) {
                this.imgArrays=[];
                axios.get(this.backend_url + "/tPhyexamrecord/findById?id=" + row.id).then((response) => {
                    if (response.data.flag) {
                        //请求成功
                        //弹出编辑窗口
                        this.dialogFormVisible4Edit = true;
                        //表单重置
                        this.resetForm();
                        // 数据回显
                        this.formData = response.data.data;
                        //多个图片回显处理
                        var arr=this.formData.report;
                        // debugger
                        if (arr!==null && arr !== undefined && arr.length >0){
                            // this.imgArrays=JSON.parse(arr);
                           var as= JSON.parse(arr);
                           for(var i=0;i<as.length;i++){
                               this.$set(this.imgArrays,i,as[i]);
                            }
                        }
                    } else {
                        //请求失败
                        this.$message.error(response.data.message);
                    }
                }).catch((error) => {
                    console.log(error);
                    this.$message.error("编辑体检数据回显错误");
                });
            },
            //分页查询
            findPage(pageNum) {
                if (pageNum) {
                    this.pagination.currentPage = pageNum;
                }
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString,
                    orderStatus: "报告未上传"
                };
                axios.get(this.backend_url + "/tPhyexamrecord/findpage", {params: param}).then((response) => {
                    if (response.data.flag) {
                        //请求成功
                        this.pagination.total = response.data.data.total;
                        this.dataList = response.data.data.rows;
                    } else {
                        //请求失败
                        this.$message.error(response.data.message);
                    }
                }).catch((error) => {
                    console.log(error);
                    this.$message.error("网络异常");
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {}
            },
            // 弹出添加窗口
            /*handleCreate() {
                this.formData = {};
                this.dialogFormVisible = true
            },*/
            //切换页码
            handleCurrentChange(currentPage) {
                this.findPage(currentPage)
            },
            // 删除
            handleDelete(row) {
                this.$confirm("您确定要删除该报告记录吗", "提示", {type: "warning"}).then(() => {
                    //点击确定后发请求
                    axios.post(this.backend_url + "/tPhyexamrecord/delete?id=" + row.id).then((response) => {
                        if (response.data.flag) {
                            //请求成功
                            this.$message.success(response.data.message);
                            this.findPage(1);
                        } else {
                            //请求失败
                            this.$message.error(response.data.message);
                        }
                    }).catch((error) => {
                        console.log(error);
                        this.$message.error("删除该项错误");
                    });
                })
            }
        }
    })
</script>
</html>
